<template>
  <vab-dialog v-model="dialogFormVisible" append-to-body :title="title" width="500px" @close="close">
    <el-form ref="formRef" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="中文名" prop="accountName">
        <el-input v-model.trim="form.accountName" clearable />
      </el-form-item>
      <el-form-item label="英文名" prop="enName">
        <el-input v-model.trim="form.enName" clearable />
      </el-form-item>
      <!-- <el-form-item label="邮箱" prop="email">
        <el-input v-model.trim="form.email" clearable :disabled="form.accountId" />
      </el-form-item> -->
      <el-form-item label="性别" prop="sex">
        <el-select v-model="form.sex" placeholder="请选择">
          <el-option v-for="item in GenderEnum" :key="item.id" :label="item.label" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model.trim="form.address" clearable />
      </el-form-item>
      <el-form-item label="部门身份" prop="departmentIdentity">
        <el-select v-model="form.departmentIdentity" placeholder="请选择">
          <el-option v-for="item in identityEnum" :key="item.id" :label="item.label" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="角色" prop="roleIds">
        <el-select v-model="form.roleIds" multiple placeholder="请选择">
          <el-option v-for="item in roleList" :key="item.roleId" :label="item.name" :value="item.roleId" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门" prop="departmentIds">
        <el-cascader v-model="form.departmentIds" clearable :options="deparmentList" :props="deparmentProps" :show-all-levels="false" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </vab-dialog>
</template>

<script lang="ts" setup>
import { doEdit } from '/@/api/account'
import { getDepartmentList, getRoleList } from '/@/api/sys'
import { GenderEnum, identityEnum } from '/@/utils/EnumUtil'

defineOptions({
  name: 'Edit',
})

const emit = defineEmits(['fetch-data'])
const $baseMessage = inject<any>('$baseMessage')
const formRef = ref<any>(null)
const title = ref<string>('')
const dialogFormVisible = ref<boolean>(false)
const form = reactive<any>({
  accountName: '',
  enName: '',
  email: '',
  sex: 0,
  address: '',
  departmentIdentity: 0,
  roleIds: [],
  departmentIds: [],
  count: 0,
})
const validateName = (rule, value, callback) => {
  if (!form.accountName && !form.enName) {
    callback(new Error('中文名和英文名有一个必填'))
  } else {
    callback()
  }
}
const roleList = ref<any>([])
const deparmentList = ref<any>([])
const deparmentProps = ref<any>({
  multiple: true,
  value: 'departmentId',
  label: 'name',
  children: 'childrenList',
  expandTrigger: 'hover',
})

const rules = reactive<any>({
  accountName: [{ trigger: 'blur', validator: validateName }],
  enName: [{ trigger: 'blur', validator: validateName }],
  sex: [{ required: true, trigger: 'change', message: '性别必须选择' }],
  departmentIdentity: [{ required: true, trigger: 'change', message: '部门身份必须选择' }],
  roleIds: [{ required: true, trigger: 'change', message: '角色必须选择' }],
  departmentIds: [{ required: true, trigger: 'change', message: '部门必须选择' }],
})

const showEdit = (row: any) => {
  dialogFormVisible.value = true
  nextTick(() => {
    if (!row) title.value = '添加'
    else {
      title.value = '编辑'
      Object.assign(form, row)
    }
  })
}

defineExpose({
  showEdit,
})

const close = () => {
  formRef.value.clearValidate()
  formRef.value.resetFields()
  emit('fetch-data')
  dialogFormVisible.value = false
}

const save = () => {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      const { message }: any = await doEdit(form)
      $baseMessage(message, 'success', 'hey')
      close()
    }
  })
}

const fetchRoleData = async () => {
  const { data } = await getRoleList()
  roleList.value = data
}

const fetchDeparmentData = async () => {
  const { data } = await getDepartmentList()
  deparmentList.value = data
}
onBeforeMount(() => {
  fetchRoleData()
  fetchDeparmentData()
})
</script>
